package com.wx.chapter17.syn;

/**
 * @Author : 佩萁
 * Description :
 * @Create : 2023/8/21 - 10:18
 */
public class DeadLock_ {
    public static void main(String[] args) {
        DeadLockDemo A = new DeadLockDemo(true);
        DeadLockDemo B = new DeadLockDemo(false);
        A.start();
        B.start();
    }
}

class DeadLockDemo extends Thread {
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;

    public DeadLockDemo(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if (flag) {
            synchronized (o1) {
                System.out.println(Thread.currentThread().getName() + "  进入 1");
                synchronized (o2) {
                    System.out.println(Thread.currentThread().getName() + "  进入 2");
                }
            }
        } else {
            synchronized (o2) {
                System.out.println(Thread.currentThread().getName() + "  进入 3");
                synchronized (o1) {
                    System.out.println(Thread.currentThread().getName() + "  进入 4");
                }
            }
        }
    }
}